import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_edit_profile/lf_manage_photos/lf_public_photo_view.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/widgets/lf_app_bar.dart';

import '../../../res/lf_text_style.dart';
import 'lf_manage_photos_logic.dart';
import 'lf_private_photo_view.dart';

class LfManagePhotosPage extends StatefulWidget {
  const LfManagePhotosPage({super.key});

  @override
  State<LfManagePhotosPage> createState() => _LfManagePhotosPageState();
}

class _LfManagePhotosPageState extends State<LfManagePhotosPage>
    with SingleTickerProviderStateMixin {
  final logic = Get.put(LfManagePhotosLogic());

  late TabController _tabController;
  late PageController _pageController;

  final RxInt _currentIndex = 0.obs;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
        length: 2,
        vsync: this,
        initialIndex: logic.type == PhotoType.private ? 1 : 0);
    _currentIndex.value = logic.type == PhotoType.private ? 1 : 0;

    _pageController = PageController(initialPage: _currentIndex.value);
  }

  @override
  void dispose() {
    _tabController.dispose();
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: lfAppBar(
              leadingCallback: () {
                Get.back();
              },
              title: strManagePhotos.tr),
          body: Column(
            children: [
              TabBar(
                tabs: [
                  Text(strPublic.tr),
                  Text(strPrivate.tr),
                ],
                onTap: (index) {
                  _currentIndex.value = index;
                  _pageController.jumpToPage(_currentIndex.value);
                },
                controller: _tabController,
                indicator: null,
                indicatorColor: colorFF007F,
                indicatorPadding: EdgeInsets.zero,
                indicatorSize: TabBarIndicatorSize.label,
                indicatorWeight: 2.h,
                labelStyle: lfTextStyle(
                    sp: 16.sp, fontFamily: fontBold, color: colorFF007F),
                unselectedLabelStyle: lfTextStyle(
                    sp: 16.sp,
                    fontFamily: fontBold,
                    color: color514F62),
              ),
              Expanded(
                child: PageView(
                  controller: _pageController,
                  onPageChanged: (index) {
                    _currentIndex.value = index;
                    _tabController.animateTo(index);
                  },
                  children: const [LfPublicPhotoView(), LfPrivatePhotoView()],
                ),
              )
            ],
          ),
        ),
        onWillPop: () async {
          Get.back();
          return false;
        });
  }
}
